<?php
namespace app\service;
use alipay\AlipayEbppInvoiceInfoSendRequest;
use alipay\AlipayEbppInvoiceMerchantlistEnterApplyRequest;
use alipay\AlipaySystemOauthTokenRequest;
use alipay\AlipayTradeCreateRequest;
use alipay\AopClient;

//换取授权访问令牌
class apiinvoiceService{
    const PRIKEYPATH = '/www/wwwroot/zaozao/path/invoice/prikey.txt';//私钥路径
    const PUBKEYPATH = '/www/wwwroot/zaozao/path/invoice/pubkey.txt';//支付宝公钥路径

    private $app_id;//支付宝分配给开发者的应用
    private $method;//接口名称
    private $format;//仅支持JSON  no
    private $charset = 'UTF-8';//请求使用的编码格式，如utf-8,gbk,gb2312等
    private $sign_type = 'RSA2';//商户生成签名字符串所使用的签名算法类型，目前支持RSA2和RSA，推荐使用RSA2
    private $sign;//商户请求参数的签名串  no
    private $timestamp;//发送请求的时间，格式"yyyy-MM-dd HH:mm:ss"
    private $version = '1.0';//调用的接口版本，固定为：1.0
    private $app_auth_token;//详见应用授权概述 no
    private $biz_content;//请求参数的集合，最大长度不限，除公共参数外所有请求参数都必须放在这个参数中传递，具体参照各产品快速接入文档

    //第三方应用
    public function getPublicContent(){
        $aop = new AopClient ();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = '2017102609541411';
        $aop->rsaPrivateKey = file_get_contents(self::PRIKEYPATH);//私钥路径
        $aop->alipayrsaPublicKey= file_get_contents(self::PUBKEYPATH);//支付宝公钥路径
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        return $aop;
    }
   

    

    //换取授权访问令牌
    public function oauthToken($code){
        $aop = $this->lifegetPublicContent();
        $request = new AlipaySystemOauthTokenRequest ();
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $request->setRefreshToken('');
        $result = $aop->execute ( $request);
        if(property_exists($result,'error_response')){
            $responseNode = 'error_response';
        }else{
            $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";   
        }
        return $result->$responseNode;
    }
    
    
    public function merchantapply($data){
        $data = json_decode($data);
        $aop = $this->getPublicContent(); 
        $request = new AlipayEbppInvoiceMerchantlistEnterApplyRequest ();
        
        $request->setBizContent("{" .
        "\"merchant_base\":{" .
            "\"m_short_name\":\"$data->m_short_name\"," .
            "\"m_name\":\"$data->m_name\"," .
            "\"logo_info\":\"$data->logo_info\"" .
        "}," .
        "\"sub_merchant_list\":[{" .
            "\"sub_m_short_name\":\"$data->sub_m_short_name\"," .
            "\"sub_m_name\":\"$data->sub_m_name\"," .
            "\"register_no\":\"$data->register_no\"," .
            "\"pid\":\"$data->pid\"," .
            "\"industry\":\"$data->industry\"," .
            "\"extend_fields\":\"\\\"{\\\\\\\\\\\"MERCHANT_OPEN_URL\\\\\\\\\\\":\\\\\\\\\\\"$data->MERCHANT_OPEN_URL\\\\\\\\\\\"}\\\"\"" .
        "        }]," .
        "\"sub_merchant_common_info\":{" .
            "\"product_code\":\"$data->product_code\"," .
            "\"s_short_name\":\"$data->s_short_name\"" .
            " }" .
        "}");
        return $request;
        $result = $aop->execute ($request); 
        
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }
	
	public  function createinvoice($data){
		$sys_service_provider_id="2088621722997814";//    2088521066336121
		$data = json_decode($data);
        $aop = $this->getPublicContent(); 
        $request = new AlipayTradeCreateRequest ();
        $request->setBizContent("{" .
        "\"out_trade_no\":\"$data->out_trade_no\"," .
        "\"total_amount\":$data->total_amount," .
        "\"subject\":\"$data->subject\"," .
        "\"buyer_id\":\"$data->buyer_id\"," .
		"\"invoice_info\":{" . 
            "\"key_info\":{" .
				"\"is_support_invoice\":\"$data->is_support_invoice\",".
				"\"invoice_merchant_name\":\"$data->invoice_merchant_name\",".
				"\"tax_num\":\"$data->tax_num\"".
				"}" .
        "    },".
        "\"timeout_express\":\"5m\"" .
        "  }");
        
        $request->setNotifyUrl($data->notifyurl);
        $token = $data->token;
        $result = $aop->execute ($request, null, $token);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }

    


	//电子发票推送
    public function invoiceSend($data){
        $data = json_decode($data);
        $aop = $this->getPublicContent(); 
        $request = new AlipayEbppInvoiceInfoSendRequest();
        $request->setBizContent("{" .
        "\"m_short_name\":\"$data->m_short_name\"," .
        "\"sub_m_short_name\":\"$data->sub_m_short_name\"," .
        "      \"invoice_info_list\":[{" .
        "        \"apply_id\":\"$data->apply_id\"," .
        "\"invoice_code\":\"$data->invoice_code\"," .
        "\"invoice_no\":\"$data->invoice_no\"," .
        "\"invoice_date\":\"$data->invoice_date\"," .
        "\"sum_amount\":\"$data->sum_amount\"," .
        "\"ex_tax_amount\":\"$data->ex_tax_amount\"," .
        "\"tax_amount\":\"$data->tax_amount\"," .
        "          \"invoice_content\":[{" .
        "            \"item_name\":\"$data->item_name\"," .
        "\"item_no\":\"$data->item_no\"," .
        "\"item_spec\":\"$data->item_spec\"," .
        "\"item_unit\":\"$data->item_unit\"," .
        "\"item_quantity\":\"$data->item_quantity\"," .
        "\"item_unit_price\":\"$data->item_unit_price\"," .
        "\"item_ex_tax_amount\":\"$data->item_ex_tax_amount\"," .
        "\"item_tax_rate\":\"$data->item_tax_rate\"," .
        "\"item_tax_amount\":\"$data->item_tax_amount\"," .
        "\"item_sum_amount\":\"$data->item_sum_amount\"," .
        "\"row_type\":\"0\"" .
        "            }]," .
        "\"out_trade_no\":\"$data->out_trade_no\"," .
        "\"invoice_type\":\"$data->invoice_type\"," .
        "\"invoice_kind\":\"$data->invoice_kind\"," .
        "\"invoice_title\":{" .
        "\"title_name\":\"$data->title_name\"," .
        "\"payer_register_no\":\"$data->payer_register_no\"," .
        "\"payer_address_tel\":\"$data->payer_address_tel\"," .
        "\"payer_bank_name_account\":\"$data->payer_bank_name_account\"" .
        "        }," .
        "\"payee_register_no\":\"$data->payee_register_no\"," .
        "\"payee_register_name\":\"$data->payee_register_name\"," .
        "\"payee_address_tel\":\"$data->payee_address_tel\"," .
        "\"payee_bank_name_account\":\"$data->payee_bank_name_account\"," .
        "\"check_code\":\"$data->check_code\"," .
        "\"out_invoice_id\":\"$data->out_invoice_id\"," .
        "\"ori_blue_inv_code\":\"$data->ori_blue_inv_code\"," .
        "\"ori_blue_inv_no\":\"$data->ori_blue_inv_no\"," .
        "\"file_download_type\":\"$data->file_download_type\"," .
        "\"file_download_url\":\"$data->file_download_url\"," .
        "\"payee\":\"$data->payee\"," .
        "\"checker\":\"$data->checker\"," .
        "\"clerk\":\"$data->clerk\"," .
        "\"invoice_memo\":\"$data->invoice_memo\"," .
        "\"extend_fields\":\"$data->xtend_fields\"" .
        "        }]" .
        "  }");
        $result = $aop->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }

    public function invoiceSendDirect($data){
        $data = json_decode($data);
        $aop = $this->getPublicContent();
        $request = new AlipayEbppInvoiceInfoSendRequest();
        $str="{" .
            "\"m_short_name\":\"$data->m_short_name\"," .
            "\"sub_m_short_name\":\"$data->sub_m_short_name\"," .
            "      \"invoice_info_list\":[{" .
            "        \"user_id\":\"$data->user_id\"," .
            "\"invoice_code\":\"$data->invoice_code\"," .
            "\"invoice_no\":\"$data->invoice_no\"," .
            "\"invoice_date\":\"$data->invoice_date\"," .
            "\"sum_amount\":\"$data->sum_amount\"," .
            "\"ex_tax_amount\":\"$data->ex_tax_amount\"," .
            "\"tax_amount\":\"$data->tax_amount\"," .
            "          \"invoice_content\":[".$data->item."]," .
            "\"out_trade_no\":\"$data->out_trade_no\"," .
            "\"invoice_type\":\"$data->invoice_type\"," .
            "\"invoice_kind\":\"$data->invoice_kind\"," .
            "\"invoice_title\":{" .
            "\"title_name\":\"$data->title_name\"," .
            "\"payer_register_no\":\"$data->payer_register_no\"," .
            "\"payer_address_tel\":\"$data->payer_address_tel\"," .
            "\"payer_bank_name_account\":\"$data->payer_bank_name_account\"" .
            "        }," .
            "\"payee_register_no\":\"$data->payee_register_no\"," .
            "\"payee_register_name\":\"$data->payee_register_name\"," .
            "\"payee_address_tel\":\"$data->payee_address_tel\"," .
            "\"payee_bank_name_account\":\"$data->payee_bank_name_account\"," .
            "\"check_code\":\"$data->check_code\"," .
            "\"out_invoice_id\":\"$data->out_invoice_id\"," .
            "\"ori_blue_inv_code\":\"$data->ori_blue_inv_code\"," .
            "\"ori_blue_inv_no\":\"$data->ori_blue_inv_no\"," .
            "\"file_download_type\":\"$data->file_download_type\"," .
            "\"file_download_url\":\"$data->file_download_url\"," .
            "\"payee\":\"$data->payee\"," .
            "\"checker\":\"$data->checker\"," .
            "\"clerk\":\"$data->clerk\"," .
            "\"invoice_memo\":\"$data->invoice_memo\"," .
            "\"extend_fields\":\"$data->xtend_fields\"" .
            "        }]" .
            "  }";
        //return $str;
        $request->setBizContent($str);
        $result = $aop->execute ( $request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        return $result->$responseNode;
    }
    
}